<?php include_once './application/views/sys_admin/common/header.php';  ?>
<div class=" definewidth m20">
		<form  class="form-inline definewidth m20">
			<button type="button" class="btn btn-success"  onclick="addRule();" >添加</button>
	  	</form>
		<table class="table table-bordered table-hover definewidth m10">
			 <thead>
			    <tr>
			      <th><strong>ID</strong></th>
					<th><strong>积分名称</strong></th>
					<th><strong>积分代号</strong></th>
					<th><strong>积分数值</strong></th>
					<th><strong>操作</strong></th>
			    </tr>
			   </thead>
			  <?php if($list){foreach ($list as $v){?>
						  	 <tr>
							<td><?php echo $v['id'];?></td>
							<td><?php echo $v['name'];?></td>
							<td><?php echo $v['nid'];?></td>
							<td><?php echo $v['credit_value'];?></td>
							<td><a dataid="<?php echo $v['id'];?>" ndata="<?php echo $v['name'];?>" cdata="<?php echo $v['credit_value'];?>" onclick="changeRule(this);" href="javascript:;">编辑</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:removeRule(<?php echo $v['id'];?>);">删除</a></td>
						  </tr>	
						  <?php }}?>
			  	<tr>
			  	<td colspan="12"  class="invbd_page"><?php echo $this->pagination->create_links();?></td>
			  	</tr>
		</table>
</div>
<div id="dialog" title="系统提示" style="display:none;">
<div id="msg" style="text-align:center;color:red">&nbsp;</div>
<input type="hidden" name="id" id="id" />
	<table class="table table-bordered table-hover m10">
		 <tr>
	        <td class="tableleft">积分名称 </td>
	        <td><input type="text" name="name" id="name" />  </td>
	   	</tr>
	   	<tr>
	        <td class="tableleft">积分数值 </td>
	        <td><input type="text" name="credit_value" id="credit_value" />  </td>
	   	</tr>
	</table>   	


<div style="text-align:center;">
<input type="button" style="width: 60px; height:24px" value="关闭" onclick="$('#dialog').dialog('close');"/>
<input type="button" style="width: 60px; height:24px" value="修改" onclick="submitRule();" />
</div>
</div>


<div id="adddialog" title="系统提示" style="display:none;">
	<div id="addmsg" style="text-align:center;color:red">&nbsp;</div>

	<table class="table table-bordered table-hover m10">
		 <tr>
	        <td class="tableleft">积分名称 </td>
	        <td><input type="text" name="credit_new_name" id="credit_new_name" />  </td>
	   	</tr>
	   	<tr>
	        <td class="tableleft">积分代号 </td>
	        <td><input type="text" name="credit_new_nid" id="credit_new_nid" />  </td>
	   	</tr>
	   	<tr>
	        <td class="tableleft">积分数值 </td>
	        <td><input type="text" name="credit_new_value" id="credit_new_value" />  </td>
	   	</tr>
	</table> 
<div style="text-align:center;">
<input type="button" style="width: 60px; height:24px" value="关闭" onclick="$('#adddialog').dialog('close');"/>

<input type="button" style="width: 60px; height:24px" value="添加" onclick="addsubmitRule();" />
</div>
</div>

<script>
function changeRule(_this){
	var id = $(_this).attr("dataid");
	if(id>0){
		$( "#dialog" ).dialog({
			height:250,
			width:500,
			modal:true, //蒙层（弹出会影响页面大小）
			title:'编辑积分规则'
		});
		
		$( "#id").val(id);
		$("#name").val($(_this).attr("ndata"));
		$("#credit_value").val($(_this).attr("cdata"));
	}
}

function submitRule(){
	var id = $("#id").val();
	if(id > 0){
		$.ajax({
			type: "POST",
			url: "/sys_admin/credit/credit_rule_save",
			data: "act=edit&id="+id+"&name="+$("#name").val()+"&credit_value="+$("#credit_value").val(),
			success: function(ret){
				$("#dialog" ).dialog();
				$("#msg").html(ret.msg);
				setTimeout(function(){
					$("#dialog" ).dialog("close");
				},3000);
				window.location.reload();
			},
			dataType: "json"
		});	
	}
}

function addRule(){
	$( "#adddialog" ).dialog({
		height:280,
		width:500,
		modal:true, //蒙层（弹出会影响页面大小）
		title:'添加积分规则'
	});
}

function addsubmitRule(){
	if($("#credit_new_name").val() == ""){
		$("#adddialog" ).dialog();
		$("#addmsg").html('请输入积分名称');
		return false;
	}

	if($("#credit_new_nid").val() == ""){
		$("#adddialog" ).dialog();
		$("#addmsg").html('请输入积分代号');
		return false;
	}

	if($("#credit_new_value").val() == ""){
		$("#adddialog" ).dialog();
		$("#addmsg").html('请输入积分分值');
		return false;
	}
	
		$.ajax({
			type: "POST",
			url: "/sys_admin/credit/credit_rule_save",
			data: "act=add&name="+$("#credit_new_name").val()+"&nid="+$("#credit_new_nid").val()+"&credit_value="+$("#credit_new_value").val(),
			success: function(ret){
				$("#adddialog" ).dialog();
				$("#addmsg").html(ret.msg);
				setTimeout(function(){
					$("#adddialog" ).dialog("close");
				},3000);
				window.location.reload();
			},
			dataType: "json"
		});	
}

function removeRule(id){
	if(confirm('你确定要删除?'))
	{
		if(id > 0){
			$.ajax({
				type: "POST",
				url: "/sys_admin/credit/credit_rule_save",
				data: "act=delete&id="+id,
				success: function(ret){
					window.location.reload();
				},
				dataType: "json"
			});	
		}	
	}	
}
</script>
